package org.example;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.ftp.Ftp;
import cn.hutool.json.JSONObject;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FtpMain {

    public static void main(String[] args) {
        getDataByDate("20250305");
    }

    private static byte[] getBytes(String fileName) {
        byte[] bytes = null;
        Ftp ftp = new Ftp("host", 21, "user", "pwd");
        for (String l : ftp.ls("")) {
            System.out.println(l);
        }
        ftp.cd("/build");
        if (ftp.existFile(fileName)) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ftp.download("/build", fileName, byteArrayOutputStream);
            bytes = byteArrayOutputStream.toByteArray();
        }
        try {
            ftp.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bytes;
    }

    public static List<JSONObject> getDataByDate(String date) {
        List<JSONObject> list = new ArrayList<>();
        String fileName1 = "89837028999ARY5_" + date + "_9.xls";
        List<JSONObject> list1 = analysisExcel(fileName1, "商户订单号", "交易金额");
        String fileName2 = "89837028999ARY5_" + date + "_68.xls";
        List<JSONObject> list2 = analysisExcel(fileName2, "商户订单号", "交易金额");
        list.addAll(list1);
        list.addAll(list2);
        return list;
    }

    private static List<JSONObject> analysisExcel(String fileName, String billNoName, String totalAmountName) {
        List<JSONObject> list = new ArrayList<>();
        byte[] bytes = getBytes(fileName);
        if (bytes != null) {
            EasyExcel.read(new ByteArrayInputStream(bytes)).registerReadListener(new AnalysisEventListener<Map<Integer, String>>() {
                Integer billNoIndex;

                Integer totalAmountIndex;

                public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
                    if (context.readRowHolder().getRowIndex() == 2) {
                        headMap.forEach((index, value) -> {
                            if (StrUtil.equals(value, billNoName)) {
                                billNoIndex = index;
                            } else if (StrUtil.equals(value, totalAmountName)) {
                                totalAmountIndex = index;
                            }
                        });
                    }
                }

                @Override
                public void invoke(Map<Integer, String> map, AnalysisContext analysisContext) {
                    if (ObjectUtil.isNotNull(map.get(billNoIndex)) && ObjectUtil.isNotNull(map.get(totalAmountIndex))) {
                        list.add(new JSONObject() {{
                            set("billNo", map.get(billNoIndex));
                            set("totalAmount", map.get(totalAmountIndex));
                        }});
                    }
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {

                }
            }).headRowNumber(3).doReadAll();

        }
        return list;
    }

}
